#ifndef SERIALIZE_DESERIALIZE_DNS_NAMES
#define SERIALIZE_DESERIALIZE_DNS_NAMES


/*
 * The following two methods can be used to serialize/deserialize a character array 
 * into a compressed name form used by DNS.
 *
 * Note that for the two methods to work correctly, you have to pass them the 
 * appropriate pointers (e.g. to the beginning of the name in the packet). 
 * Passing arbitrary pointers and sizes as parameters may make your programs crash.
 */



// --------------------------------------------------------------------------
/*
 * deserialize_DNS_name:
 *
 * Transforms a compressed name into a NULL terminated char pointer.
 *
 * Parameters:
 * 	input: input_buf = pointer to the input buffer from which to deserialize.
 * 	input: input_buf_size = size of the input_buf buffer.
 * 	output: read_size = a pointer to an integer which will be updated with 
 *		the value of the number of characters read from the input buffer.
 *
 * Returned Value:
 * 	The function returns a pointer to a valid NULL terminated array of characters
 *	or NULL in case of failure.
 * 	Each valid response name is dynamically allocated within the function and you 
 *	should free it when it is no longer needed.
 *
 */

char* deserialize_DNS_name(const unsigned char* input_buf, unsigned short input_buf_size, unsigned short* size_read);


// --------------------------------------------------------------------------

/*
 * serialize_DNS_name:
 *
 * Writes a given name into a buffer using the name format used by DNS.
 *
 * Parameters:
 * 	input: output_buf = pointer to the output buffer from which the function reads.
 * 	input: output_buf_size = size of the output buffer. 
 * 	output: written_size = the number of bytes written in output_buf;
 *
 * Returned Value:
 * 	The function returns 0 if it fails or 1 if it succeeds.
 *	
 *	For the function to succeed, the size of the output buffer has to be (at least) 
 *	the length of the name + 2 bytes. 
 *	The function can fail if: input parameters are null, there is no enough room to 
 *	write in the buffer, a name segment (name between two dots) is longer than 63 
 *	characters, or the name has two consecutive dots or starts with a dot.
 *
 */

int serialize_DNS_name(unsigned char* output_buf, unsigned short output_buf_size, unsigned short* size_written, const char* name);


// --------------------------------------------------------------------------


#endif
